<template>
  <div class="app-container">
    <el-form ref="queryForm" :model="queryParams" :inline="true" size="small" label-width="90px" v-show="showSearch">
      <el-form-item label="调拨单号" prop="orderNo">
        <el-input v-model="queryParams.orderNo" placeholder="请输入调拨单号" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="调拨类型" prop="transferType">
        <el-input v-model="queryParams.transferType" placeholder="请输入调拨类型" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="状态" prop="orderStatus">
        <el-input v-model="queryParams.orderStatus" placeholder="请输入状态" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
                   v-hasPermi="['wms:transfer:add']">新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
                   v-hasPermi="['wms:transfer:edit']">编辑
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
                   v-hasPermi="['wms:transfer:remove']">删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
                   v-hasPermi="['wms:transfer:export']">导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"/>
    </el-row>

    <el-table v-loading="loading" :data="transferList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="调拨单号" prop="orderNo" min-width="160"/>
      <el-table-column label="调拨类型" prop="transferType" width="120"/>
      <el-table-column label="源仓库" prop="sourceWarehouseId" width="120"/>
      <el-table-column label="目标仓库" prop="targetWarehouseId" width="120"/>
      <el-table-column label="调拨状态" prop="orderStatus" width="130"/>
      <el-table-column label="计划执行" prop="planExecuteTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.planExecuteTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="150">
        <template #default="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
                     v-hasPermi="['wms:transfer:edit']">编辑
          </el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
                     v-hasPermi="['wms:transfer:remove']">删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
                @pagination="getList"/>

    <el-dialog :title="title" :visible.sync="open" width="900px" append-to-body :close-on-click-modal="false">
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-row :gutter="10">
          <el-col :span="12">
            <el-form-item label="调拨单号" prop="orderNo">
              <el-input v-model="form.orderNo" placeholder="请输入调拨单号"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="调拨类型" prop="transferType">
              <el-input v-model="form.transferType" placeholder="请输入调拨类型"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="源仓库ID" prop="sourceWarehouseId">
              <el-input-number v-model="form.sourceWarehouseId" :min="0" controls-position="right"
                               style="width: 100%"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="目标仓库ID" prop="targetWarehouseId">
              <el-input-number v-model="form.targetWarehouseId" :min="0" controls-position="right"
                               style="width: 100%"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="源库区ID" prop="sourceZoneId">
              <el-input-number v-model="form.sourceZoneId" :min="0" controls-position="right" style="width: 100%"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="目标库区ID" prop="targetZoneId">
              <el-input-number v-model="form.targetZoneId" :min="0" controls-position="right" style="width: 100%"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="计划执行" prop="planExecuteTime">
              <el-date-picker v-model="form.planExecuteTime" type="datetime" value-format="yyyy-MM-dd HH:mm:ss"
                              placeholder="请选择计划执行时间" style="width: 100%"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="调拨状态" prop="orderStatus">
              <el-input v-model="form.orderStatus" placeholder="请输入调拨状态"/>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="备注" prop="description">
              <el-input type="textarea" v-model="form.description" placeholder="请输入备注" :rows="3"/>
            </el-form-item>
          </el-col>
        </el-row>

        <el-divider>调拨明细</el-divider>
        <el-button type="primary" size="mini" icon="el-icon-plus" @click="addDetailRow">新增明细</el-button>
        <el-table :data="form.details" size="small" style="margin-top: 10px">
          <el-table-column label="行号" type="index" width="60"/>
          <el-table-column label="SKU ID" width="120">
            <template #default="scope">
              <el-input-number v-model="scope.row.skuId" :min="0" controls-position="right" size="small"/>
            </template>
          </el-table-column>
          <el-table-column label="批次号" width="150">
            <template #default="scope">
              <el-input v-model="scope.row.batchNo" size="small" placeholder="批次号"/>
            </template>
          </el-table-column>
          <el-table-column label="源货位" width="120">
            <template #default="scope">
              <el-input-number v-model="scope.row.sourceLocationId" :min="0" controls-position="right" size="small"/>
            </template>
          </el-table-column>
          <el-table-column label="目标货位" width="120">
            <template #default="scope">
              <el-input-number v-model="scope.row.targetLocationId" :min="0" controls-position="right" size="small"/>
            </template>
          </el-table-column>
          <el-table-column label="调拨数量" width="120">
            <template #default="scope">
              <el-input-number v-model="scope.row.transferQuantity" :min="0" controls-position="right" size="small"/>
            </template>
          </el-table-column>
          <el-table-column label="操作" width="100">
            <template #default="scope">
              <el-button type="text" size="small" icon="el-icon-delete" @click="removeDetailRow(scope.$index)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">保存</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {addTransfer, delTransfer, getTransfer, listTransfer, updateTransfer} from '@/api/wms/transfer'

export default {
  name: 'WmsTransfer',
  data() {
    return {
      loading: false,
      showSearch: true,
      total: 0,
      transferList: [],
      ids: [],
      single: true,
      multiple: true,
      open: false,
      title: '',
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        orderNo: undefined,
        transferType: undefined,
        orderStatus: undefined
      },
      form: {},
      rules: {
        orderNo: [{required: true, message: '调拨单号不能为空', trigger: 'blur'}],
        transferType: [{required: true, message: '调拨类型不能为空', trigger: 'blur'}]
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      this.loading = true
      listTransfer(this.queryParams).then(res => {
        this.transferList = res.rows || []
        this.total = res.total || 0
        this.loading = false
      })
    },
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    reset() {
      this.form = {
        id: undefined,
        orderNo: undefined,
        transferType: 'inner',
        sourceWarehouseId: undefined,
        targetWarehouseId: undefined,
        sourceZoneId: undefined,
        targetZoneId: undefined,
        planExecuteTime: undefined,
        orderStatus: 'created',
        description: undefined,
        details: []
      }
      this.resetForm('form')
    },
    cancel() {
      this.open = false
      this.reset()
    },
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '新增调拨单'
    },
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids[0]
      getTransfer(id).then(res => {
        this.form = res.data
        if (!this.form.details) {
          this.form.details = []
        }
        this.open = true
        this.title = '编辑调拨单'
      })
    },
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (!valid) return
        const request = this.form.id ? updateTransfer(this.form) : addTransfer(this.form)
        request.then(() => {
          this.$modal.msgSuccess('保存成功')
          this.open = false
          this.getList()
        })
      })
    },
    handleDelete(row) {
      const ids = row.id ? [row.id] : this.ids
      this.$modal.confirm('是否确认删除选中的调拨单？').then(() => delTransfer(ids.join(','))).then(() => {
        this.getList()
        this.$modal.msgSuccess('删除成功')
      }).catch(() => {
      })
    },
    handleExport() {
      this.download('/wms/transfer/export', {...this.queryParams}, `transfer_${new Date().getTime()}.xlsx`)
    },
    addDetailRow() {
      this.form.details.push({
        skuId: undefined,
        batchNo: undefined,
        sourceLocationId: undefined,
        targetLocationId: undefined,
        transferQuantity: 0
      })
    },
    removeDetailRow(index) {
      this.form.details.splice(index, 1)
    }
  }
}
</script>

